package org.jeecg.modules.yw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.yw.entity.Attendapprove;
import org.jeecg.modules.yw.mapper.AttendapproveMapper;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.IAttendapproveService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 考勤审批
 * @Author: jeecg-boot
 * @Date:   2025-04-10
 * @Version: V1.0
 */
@Service
public class AttendapproveServiceImpl extends ServiceImpl<AttendapproveMapper, Attendapprove> implements IAttendapproveService {

    @Resource
    private AttendapproveMapper AttendapproveMapper;
    @Resource ISysUserService sysUserService;
    @Override
    public Result addAttendApprove(Attendapprove attendapprove) {
        if (attendapprove.getWorkid() == null||attendapprove.getStartTime()==null||attendapprove.getStopTime()==null||
        attendapprove.getWorkid().equals("")||attendapprove.getStartTime().equals("")||attendapprove.getStopTime().equals("")){
            return Result.error("开始时间、结束时间和工号不能为空，请输入正确的。");
        }
        Date starTimeDate = attendapprove.getStartTime();
        Date stopTimeDate = attendapprove.getStopTime();
        if (starTimeDate.after(stopTimeDate)){
                return Result.error( "开始时间不能大于结束时间，请重新选择时间。");
        }
        QueryWrapper<Attendapprove> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workid", attendapprove.getWorkid())
                .eq("approve", "0")
                .and(wrapper -> wrapper
                        .lt("startTime", stopTimeDate)
                        .gt("stopTime", starTimeDate)
                );
        Long l = AttendapproveMapper.selectCount(queryWrapper);
        if (l>0){
            return Result.error("您有正在审批的请假记录，请等待审批结果。");
        }
        attendapprove.setId(CommonUtils.getID());
        attendapprove.setType("0");
        attendapprove.setApprove("0");
        attendapprove.setCreatetime(DateUtils.getCurrentDate());
        attendapprove.setStartTime(starTimeDate);
        attendapprove.setStopTime(stopTimeDate);
        AttendapproveMapper.insert(attendapprove);
        return Result.success("请假申请成功");
    }
    @Override
    public Result queryByEnterPriseId(IPage<Attendapprove> page, String enterPriseId){
        QueryWrapper<Attendapprove> queryWrapper = new QueryWrapper();
        queryWrapper.eq("workid",enterPriseId);
        queryWrapper.orderByDesc("approvetime");
        //我想把传来的page 转成Page
        Page page1 = new Page<>(page.getCurrent(), page.getSize());
        IPage resultpage = AttendapproveMapper.selectMapsPage(page1, queryWrapper);
        //对resultpage里数据进行操作
        List<Map<String, Object>> records = resultpage.getRecords();
        for (Map<String, Object> record : records){
            String id = (String) record.get("approveman");
            SysUser user = sysUserService.getById(id);
            if (user!=null){
                record.put("realName",user.getRealname());
            }
        }
        return Result.success(resultpage);

    }
}

